package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理端 - 报警规则相关接口
 * @Description AlertRuleController
 * @Author mks
 * @Date 2024-09-14
 */
@Slf4j
@RestController
@RequestMapping("/alert-rule")
@Api(tags = "管理端 - 报警规则相关接口")
public class AlertRuleController extends BaseController {

    @Autowired
    private AlertRuleService alertRuleService;

    @PostMapping("/create")
    @ApiOperation(value = "新增报警规则")
    public ResponseResult addAlertRule(@RequestBody AlertRuleDto alertRuleDto){
        alertRuleService.addAlertRule(alertRuleDto);
        return success();
    }

    @GetMapping("/get-page")
    @ApiOperation(value = "分页获取警告规则列表")
    public ResponseResult<PageResponse<AlertRuleVo>> getPage(
            @ApiParam(value = "页码", required = true) @RequestParam("pageNum") Integer pageNum,
            @ApiParam(value = "每页大小", required = true) @RequestParam("pageSize") Integer pageSize,
            @ApiParam(value = "规则名称，精确查询") @RequestParam(value = "alertRuleName", required = false) String alertRuleName,
            @ApiParam(value = "功能名称，精确查询") @RequestParam(value = "functionName", required = false) String functionName,
            @ApiParam(value = "产品ID") @RequestParam(value = "productKey", required = false) String productKey){
        PageResponse<AlertRuleVo> pageResponse = alertRuleService.getPage(pageNum,pageSize,alertRuleName,functionName,productKey);
        return success(pageResponse);
    }

    @GetMapping("/read/{id}")
    @ApiOperation(value = "获取单个告警规则")
    public ResponseResult getAlertRuleId(@PathVariable Long id){
        AlertRuleVo alertRuleVo = alertRuleService.getAlertRuleId(id);
        return success(alertRuleVo);
    }

    @PutMapping("/update/{id}")
    @ApiOperation(value = "更新告警规则")
    public ResponseResult updateAlertRuleId(@PathVariable Long id, @RequestBody AlertRuleDto alertRuleDto){
        alertRuleService.updateAlertRuleId(id, alertRuleDto);
        return success();
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation(value = "启用-禁用告警")
    public ResponseResult updateAlertRuleStatus(@PathVariable Long id, @PathVariable Integer status){
        alertRuleService.updateAlertRuleStatus(id,status);
        return success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除告警规则")
    public ResponseResult deleteAlertRuleId(@PathVariable Long id){
        alertRuleService.deleteAlertRuleId(id);
        return success();
    }
}
